//
//  JQFXHAdreesttPicker.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/12.
//

import Foundation
import UIKit

typealias JQFXHAddressBlock = (String, String, String)->Void

class JQFXHAddressPickerViewController: UIViewController {
    var jqfxhAddresskkBlock : JQFXHAddressBlock?
    var jqfxhSelectffProvince = "北京"
    var jqfxhSelectjjArea = "东城区"
    var jqfxhSelecthhCity = "北京"
    var jqfxhProvincepps : [JQFXHProvinceccModel] = []

    @objc func jqfxhCancelwwBtnClick() {
        self.dismiss(animated: true)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.jqfxhLoadAddressqqData()
        
        self.view.addSubview(jqfxhContentrrView)
        jqfxhContentrrView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view.snp.centerY)
        }
        jqfxhContentrrView.addSubview(jqfxhTitleuuLabel)
        jqfxhTitleuuLabel.snp.makeConstraints { make in
            make.centerX.equalTo(jqfxhContentrrView)
            make.top.equalTo(jqfxhContentrrView).offset(10)
            make.height.equalTo(32)
        }
        
        jqfxhContentrrView.addSubview(jqfxhCanclevvBtn)
        jqfxhCanclevvBtn.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(28)
            make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom-30)
            make.height.equalTo(37)
        }
        
        jqfxhContentrrView.addSubview(jqfxhQuewwButton)
        jqfxhQuewwButton.snp.makeConstraints { make in
            make.right.equalTo(self.view).offset(-28)
            make.left.equalTo(jqfxhCanclevvBtn.snp.right).offset(30)
            make.width.equalTo(jqfxhCanclevvBtn)
            make.bottom.height.equalTo(self.jqfxhCanclevvBtn)
        }
        jqfxhContentrrView.addSubview(jqfxhPickerView)
        jqfxhPickerView.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhContentrrView)
            make.top.equalTo(jqfxhTitleuuLabel.snp.bottom).offset(10)
            make.bottom.equalTo(jqfxhQuewwButton.snp.top).offset(-10)
        }

    }
    
    
    private func jqfxhLoadAddressqqData() {
       
        guard let url = Bundle.main.url(forResource: "jqfxhRegiongg", withExtension: "json"),
              let data = try? Data(contentsOf: url) else {
            return
        }
        do {
            let addressData = try JSONDecoder().decode(JQFXHAddressData.self, from: data)
            self.jqfxhProvincepps = addressData.array
        } catch {
        }

    }
    
    @objc func jqfxhQueBtnxxClick() {
        let jqfxhSelectedooProvinces = jqfxhPickerView.selectedRow(inComponent: 0)
        self.jqfxhSelectffProvince = self.jqfxhProvincepps[jqfxhSelectedooProvinces].name
        
        let selectedCity = jqfxhPickerView.selectedRow(inComponent: 1)
        self.jqfxhSelecthhCity = self.jqfxhProvincepps[jqfxhSelectedooProvinces].city[selectedCity].name
        
        let selectedArea = jqfxhPickerView.selectedRow(inComponent: 2)
        self.jqfxhSelectjjArea = self.jqfxhProvincepps[jqfxhSelectedooProvinces].city[selectedCity].area[selectedArea]
        
        self.jqfxhAddresskkBlock?(jqfxhSelectffProvince, jqfxhSelecthhCity, jqfxhSelectjjArea)
        self.dismiss(animated: true)

    }
    

    
    lazy var jqfxhContentrrView : UIView = {
        let jqfxhContentrrView = UIView(frame: CGRect(x: 0, y: jqfxhScreenHeight/2, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2))
        jqfxhContentrrView.backgroundColor = UIColor.white
        return jqfxhContentrrView
    }()
    
    
    lazy var jqfxhQuewwButton: UIButton = {
        let jqfxhQuewwButton = UIButton(type: .system)
        jqfxhQuewwButton.frame = CGRect(x: jqfxhScreenWidth/2+15, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        jqfxhQuewwButton.layer.cornerRadius = 8
        jqfxhQuewwButton.setTitle("确定", for: .normal)
        jqfxhQuewwButton.setTitleColor(.white, for: .normal)
        jqfxhQuewwButton.backgroundColor = UIColor.jqfxhThemexxRed
        jqfxhQuewwButton.addTarget(self, action: #selector(jqfxhQueBtnxxClick), for: .touchUpInside)
        return jqfxhQuewwButton
    }()
    
    lazy var jqfxhTitleuuLabel : UILabel = {
        let navjqfxhTitleuuLabel = UILabel(frame: CGRectMake(0, 0, jqfxhScreenWidth, 34))
        navjqfxhTitleuuLabel.text = "地址选择"
        navjqfxhTitleuuLabel.font = UIFont.jqfxhBold18
        navjqfxhTitleuuLabel.textColor = UIColor.black
        return navjqfxhTitleuuLabel
    }()
    
    lazy var jqfxhCanclevvBtn: UIButton = {
        let jqfxhCanclevvBtn = UIButton(type: .system)
        jqfxhCanclevvBtn.frame = CGRect(x: 30, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        jqfxhCanclevvBtn.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        jqfxhCanclevvBtn.setTitleColor(.lightGray, for: .normal)
        jqfxhCanclevvBtn.setTitle("取消", for: .normal)
        jqfxhCanclevvBtn.layer.cornerRadius = 8
        jqfxhCanclevvBtn.addTarget(self, action: #selector(jqfxhCancelwwBtnClick), for: .touchUpInside)
        return jqfxhCanclevvBtn
    }()
    
    lazy var jqfxhPickerView : UIPickerView = {
        let jqfxhPickerView = UIPickerView(frame: CGRect(x: 0, y: 35, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2-35-UIApplication.shared.jqfxhSafeAreaInsets.bottom-40))
        jqfxhPickerView.backgroundColor = UIColor.white
        jqfxhPickerView.delegate = self
        jqfxhPickerView.dataSource = self
        return jqfxhPickerView
    }()
    
    
    
    
    
}

extension JQFXHAddressPickerViewController : UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return jqfxhProvincepps[row].name
        } else if component == 1{
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhProvincepps[selectedProvinceIndex].city[row].name
        }else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            let citys = jqfxhProvincepps[selectedProvinceIndex].city
            let selectedCityIndex = pickerView.selectedRow(inComponent: 1)
            return citys[selectedCityIndex].area[row]
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            // 当选择省份时，重置城市的选择器到默认值（省份对应的第一个城市）
            pickerView.reloadComponent(1) // 重新加载城市组件，以显示新省份的城市列表
            pickerView.selectRow(0, inComponent: 1, animated: true) // 选择默认城市
            pickerView.reloadComponent(2) // 重新加载城市组件，以显示新省份的城市列表
            pickerView.selectRow(0, inComponent: 2, animated: true) // 选择默认城市
        }else if component == 1 {
            pickerView.reloadComponent(2) // 重新加载城市组件，以显示新省份的城市列表
            pickerView.selectRow(0, inComponent: 2, animated: true) // 选择默认城市
           
        }else {
            
        }
    }
}


extension JQFXHAddressPickerViewController : UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3 
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return jqfxhProvincepps.count
        } else if component == 1{
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhProvincepps[selectedProvinceIndex].city.count
        }else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            let citys = jqfxhProvincepps[selectedProvinceIndex].city
            let selectedCityIndex = pickerView.selectedRow(inComponent: 1)
            return citys[selectedCityIndex].area.count


        }
    }
}
